package one.stand.controller;

import com.jmp.api.user.dto.GlobalConfigurationResponse;
import com.jmp.api.user.dto.HomeCardRequest;
import com.jmp.api.user.dto.HomeCardResponse;
import com.jmp.api.user.dto.HomeInfoResponse;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.*;
import com.jmp.feign.ChatFeign;
import com.jmp.feign.SecurityFeign;
import com.jmp.feign.SecurityUserFeign;
import com.jmp.im.ImNotifyClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import one.stand.dto.UserDto;
import one.stand.model.BusinessCard;
import one.stand.model.Company;
import one.stand.model.ResultModel;
import one.stand.model.Website;
import one.stand.request.GetWxPhoneRequest;
import one.stand.request.WxLoginRequest;
import one.stand.response.LoginResponse;
import one.stand.service.BusinessCardBaseService;
import one.stand.service.CompanyBaseService;
import one.stand.service.MessageMsgService;
import one.stand.service.WebsiteService;
import one.stand.service.user.UserAdminService;
import one.stand.util.AssertUtil;
import one.stand.util.RequestContext;
import one.stand.util.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/userApi/home")
@Api(description = "基础接口")
public class HomeController extends BaseController {
    @Autowired
    private SecurityFeign securityFeign;
    @Autowired
    private SecurityUserFeign securityUserFeign;
    @Autowired
    private ChatFeign chatFeign;

    @Autowired
    private MessageMsgService messageMsgService;
    @Autowired
    private BusinessCardBaseService businessCardBaseService;
    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private WebsiteService websiteService;
    @Autowired
    private UserAdminService userAdminService;

    @PostMapping("/getWxPhone")
    @ApiOperation(value = "获取微信电话")
    @SecurityAuthCheck
    public ResultModel<String> getWxPhone(@RequestAttribute GetWxPhoneRequest request) {
        RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();
        SecurityUserBindWxPhoneRequest securityUserBindWxPhoneRequest = SecurityUserBindWxPhoneRequest.builder()
                .userId(optUser.getUserId())
                .sessionKey(optUser.getSessionKey())
                .phoneData(request.getPhoneData())
                .phoneIv(request.getPhoneIv())
                .build();
        ResultModel<SecurityUserBindWxPhoneResponse> responseResultModel = securityUserFeign.userBindWxPhone(securityUserBindWxPhoneRequest);
        AssertUtil.isTrue(responseResultModel);
        return ResultModel.success(responseResultModel.getData().getPhone());
    }

    @GetMapping("initial")
    @ApiOperation(value = "获取页面初始化配置")
    public ResultModel<List<GlobalConfigurationResponse>> initial() {
        List<GlobalConfigurationResponse> responseList = new ArrayList<>();
        return ResultModel.success(responseList);
    }

    @PostMapping("/wxLogin")
    @ApiOperation(value = "微信登陆接口")
    public ResultModel<LoginResponse> wxLogin(@RequestAttribute WxLoginRequest request) {
        SecurityLoginUserApiWxRequest securityLoginUserApiWxRequest = SecurityLoginUserApiWxRequest.builder()
                .code(request.getCode())
                .companyKey(request.getCompanyKey())
                .encrypteData(request.getEncrypteData())
                .iv(request.getIv())
                .build();
        ResultModel<LoginResponse> model = securityFeign.loginUserApiWx(securityLoginUserApiWxRequest);
        AssertUtil.isTrue(model);
        if (Objects.nonNull(model.getData())) {
            Integer unReadMsg = messageMsgService.getUnreadNumAll(String.valueOf(model.getData().getUserId()), ImNotifyClient.TYPE);
            model.getData().setUnReadMsg(unReadMsg);
        }
        return model;
    }

    @PostMapping("/card")
    @ApiOperation(value = "登录名片详情")
    public ResultModel<HomeCardResponse> wxLogin(@RequestAttribute HomeCardRequest request) {

        BusinessCard businessCardModel = businessCardBaseService.getCustomer(request.getCardId());
        Company company = companyBaseService.getById(businessCardModel.getCompanyId());
        Website website = websiteService.getByCompanyId(company.getCompanyId());

        UserDto user = userAdminService.get(request.getId());

        HomeCardResponse response = HomeCardResponse.builder()
                .cardImg(businessCardModel.getLogo())
                .soundRecording(businessCardModel.getSoundRecording())
                .soundTime(businessCardModel.getSoundTime())
                .name(businessCardModel.getName())
                .position(businessCardModel.getPosition())
                .phone(businessCardModel.getPhone())
                .companyName(company.getCompanyName())
                .companyLogo(company.getCompanyLogo())
                .address(website.getAddress())
                .userName(user.getName())
                .userImg(user.getUserImg())
                .build();
        return ResultModel.success(response);
    }

    @PostMapping("/info")
    @ApiOperation(value = "信息")
    @SecurityAuthCheck
    public ResultModel<HomeInfoResponse> info() {
        RequestContext.User optUser = RequestContextHolder.getRequestContext().getUser();
        ResultModel<ChatInfoVo> chatInfoVoResultModel = chatFeign.info(ChatInfoDto.builder()
                .userId(optUser.getUserId())
                .build());
        AssertUtil.isTrue(chatInfoVoResultModel);

        HomeInfoResponse response = HomeInfoResponse.builder()
                .chatUserId(chatInfoVoResultModel.getData().getChatUserId())
                .chatPassword(chatInfoVoResultModel.getData().getChatPassword())
                .build();
        return ResultModel.success(response);
    }
}
